package com.example.yamba;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.os.AsyncTask;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.MenuInflater;

import com.sina.weibo.sdk.auth.Oauth2AccessToken;
import com.sina.weibo.sdk.net.RequestListener;
import com.sina.weibo.sdk.openapi.UsersAPI;
import com.sina.weibo.sdk.openapi.models.ErrorInfo;
import com.sina.weibo.sdk.openapi.models.User;
import com.sina.weibo.sdk.openapi.StatusesAPI;
import com.sina.weibo.sdk.openapi.models.Status;

public class StatusActivity5 extends Activity implements OnClickListener {
	private static final String TAG = "StatusActivity"; 
	private Oauth2AccessToken token;
	private UsersAPI userApi;
	private StatusesAPI statusesApi;
	EditText editText;
	Button buttonUpdate;
	TextView textView;
	TextView textCount;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.status);
		textView = (TextView) findViewById(R.id.textView1);
		textCount = (TextView) findViewById(R.id.textCount);
		editText = (EditText) findViewById(R.id.editText);
		buttonUpdate = (Button) findViewById(R.id.buttonUpdate);

		buttonUpdate.setOnClickListener(this);

		token = AccessTokenKeeper.readAccessToken(this);
		userApi = new UsersAPI(this, Constants.APP_KEY, token);
		long uid = Long.parseLong(token.getUid());
		userApi.show(uid, requestListener);
		statusesApi = new StatusesAPI(this, Constants.APP_KEY, token);

		textCount.setText(Integer.toString(140));
		textCount.setTextColor(Color.GREEN);
		editText.addTextChangedListener(new TextWatcher() {
			@Override
			public void afterTextChanged(Editable s) {
				// TODO Auto-generated method stub
				int count = 140 - s.length();
				textCount.setText(Integer.toString(count));
				textCount.setTextColor(Color.GREEN);
				if (count < 100 && count > 10)
					textCount.setTextColor(Color.CYAN);
				else if (count <= 10)
					textCount.setTextColor(Color.RED);
			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
				// TODO Auto-generated method stub

			}

		});

	}

	public void onClick(View v) {
		String text = editText.getText().toString();
		// statusesApi.update(text,null,null,statusListener);
		new PostToWeibo().execute(text);
		Log.d(TAG, "status onClick");
	}

	private RequestListener requestListener = new RequestListener() {
		public void onComplete(String request) {
			User user = User.parse(request);
			textView.setText(user.screen_name);
		};

		public void onWeiboException(
				com.sina.weibo.sdk.exception.WeiboException e) {
			ErrorInfo info = ErrorInfo.parse(e.getMessage());
			Toast.makeText(StatusActivity5.this, info.toString(),
					Toast.LENGTH_LONG).show();
		};
	};
	private RequestListener statusListener = new RequestListener() {
		public void onComplete(String request) {
			Status status = Status.parse(request);
			Toast.makeText(StatusActivity5.this, "id = " + status.id,
					Toast.LENGTH_LONG).show();
		};

		public void onWeiboException(
				com.sina.weibo.sdk.exception.WeiboException e) {
			ErrorInfo info = ErrorInfo.parse(e.getMessage());
			Toast.makeText(StatusActivity5.this, info.toString(),
					Toast.LENGTH_LONG).show();
		};
	};

	class PostToWeibo extends AsyncTask<String, Integer, String> {
		public String doInBackground(String... params) {
			statusesApi.update(params[0], null, null, statusListener);
			return "this is a post";
		}

		@Override
		protected void onProgressUpdate(Integer... values) {
			// TODO Auto-generated method stub
			super.onProgressUpdate(values);
		}

		protected void onPostExecute(String result) {
			Toast.makeText(StatusActivity5.this, result, Toast.LENGTH_LONG)
					.show();
		}
	}


    protected void onStop(){
        super.onStop();
        android.os.Process.killProcess(android.os.Process.myPid());
    }
}
